package com.awei.study.thread;

import lombok.extern.slf4j.Slf4j;

import java.lang.ref.WeakReference;

/**
 * @Author liweilong
 * @Date 2024/11/3 10:15
 */
@Slf4j
public class ThreadLocalStudy {

    static ThreadLocal<String> local = new ThreadLocal<>();

    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
//                System.out.println(this.hashCode());
//                log.info("存储t1");
                local.set("t1");
                local.get();
//                System.out.println(local);
//                local.set("t2");
//                getAndRemove();
//                log.info("t1线程获取到的ThreadLocal值" + local.get());
                System.out.println("yes1111");
            }
        },"t1");
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
//                System.out.println(this.hashCode());
                local.set("t2");
                local.get();
//                log.info("存储t2");
//                local.set("t2");
//                getAndRemove();
//                log.info("t2线程获取到的ThreadLocal值" + local.get());
            }
        },"t2");

        t1.start();
//        Thread.sleep(2000);
        t2.start();
    }

    public static void getAndRemove() {
        log.info("线程获取到的ThreadLocal值" + local.get());
//        local.remove();
    }
}
